package com.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Orders;
import com.entity.Pingjia;
import com.entity.Tour;
import com.entity.User;
import com.entity.Zixun;
import com.service.OrdersService;
import com.service.PingjiaService;
import com.service.TourService;
import com.service.UserService;
import com.service.ZixunService;
import com.util.Util;



@Controller
@RequestMapping("/manage")
public class ManageController {

	@Resource
	private UserService userService;

	@Resource
	private ZixunService zixunService;
	
	@Resource
	private TourService tourService;

	@Resource
	private OrdersService ordersService;
	
	@Resource
	private PingjiaService pingjiaService;
	
	@Value("#{jdbc.pageSize}")
	private int pageSize;


	// 获取输出对象
	public PrintWriter getPrintWriter(HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return writer;
	}



	//管理员登录
	@RequestMapping("/login.do")
	public void login(HttpServletRequest request,HttpServletResponse response,String username,String password){

		PrintWriter writer = this.getPrintWriter(response);

		User user = userService.userlogin(username, password,1);

		if(user==null){

			writer.print("<script language=javascript>alert('用户名或者密码错误');window.location.href='login.jsp';</script>");

		}else{
			HttpSession session = request.getSession();
			session.setAttribute("manage", user);

			writer.print("<script language=javascript>alert('登录成功');window.location.href='index.jsp';</script>");

		}

	}


	//安全退出
	@RequestMapping("/loginout.do")
	public void loginout(HttpServletRequest request,HttpServletResponse response,String username,String password){

		
		PrintWriter writer = this.getPrintWriter(response);

		HttpSession session = request.getSession();
		session.removeAttribute("manage");
		writer.print("<script language=javascript>alert('退出成功');window.location.href='login.jsp';</script>");

	}


	//跳转到修改密码页面
	@RequestMapping("/password.do")
	public String password(HttpServletRequest request){

		request.setAttribute("url", "password2.do");

		request.setAttribute("title", "修改密码");

		return "password.jsp";

	}

	//修改密码操作
	@RequestMapping("/password2.do")
	public void password2(HttpServletRequest request,HttpServletResponse response,String password1,String password2){

		PrintWriter writer = this.getPrintWriter(response);

		HttpSession session = request.getSession();

		User user = (User)session.getAttribute("manage");

		User bean = userService.userlogin(user.getUsername(), password1, user.getRole());

		if(bean!=null){
			bean.setPassword(password2);
			userService.updateBean(bean);

			writer.print("<script language=javascript>alert('修改成功');window.location.href='password.do';</script>");


		}else{

			writer.print("<script language=javascript>alert('用户名或者密码错误');window.location.href='password.do';</script>");

		}
	}
	
	
	
	//资讯列表
	@RequestMapping("/zixunlist.do")
	public String zixunlist(HttpServletRequest request,String pagenum,String biaoti){

		String url = "zixunlist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_zixun where  ");
		
		//查询条件返回页面
		if (biaoti != null && !"".equals(biaoti)) {

			sb.append(" biaoti like '%"+biaoti+"%' ");
			sb.append(" and ");
			
			request.setAttribute("biaoti", biaoti);
		}
		

		sb.append(" 1=1 order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Zixun>> map = zixunService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Zixun> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "zixun");

		request.setAttribute("title", "资讯管理");


		return "zixun/zixunlist.jsp";

	}


	//跳转到添加资讯页面
	@RequestMapping("/zixunadd.do")
	public String zixunadd(HttpServletResponse response,HttpServletRequest request){

		request.setAttribute("url", "zixunadd2.do");

		request.setAttribute("title", "添加资讯");

		return "zixun/zixunadd.jsp";

	}


	//添加资讯操作
	@RequestMapping("/zixunadd2.do")
	public void zixunadd2(HttpServletRequest request,HttpServletResponse response,Zixun bean){
		
		bean.setCtime(Util.getRiqi());
		bean.setVist(0);
		
		zixunService.insertBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='zixunlist.do';</script>");
	}


	//跳转到修改资讯页面
	@RequestMapping("/zixunupdate.do")
	public String zixunupdate(HttpServletRequest request,int id){

		Zixun bean = zixunService.selectData(" select * from t_zixun where id= "+id).get(0);
		
		request.setAttribute("bean", bean);

		request.setAttribute("url", "zixunupdate2.do?id="+id);
		
		request.setAttribute("title", "修改资讯");

		return "zixun/zixunupdate.jsp";

	}

	//修改资讯操作
	@RequestMapping("/zixunupdate2.do")
	public void zixunupdate2(HttpServletRequest request,HttpServletResponse response,Zixun bean){

		
		
		zixunService.updateBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='zixunlist.do';</script>");
	}


	//删除操作
	@RequestMapping("/zixundelete.do")
	public void zixundelete(HttpServletResponse response,int id){

		zixunService.deleteBean(id);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='zixunlist.do';</script>");
	}



	//跳转到查看详情页面
	@RequestMapping("/zixunshow.do")
	public String zixunshow(HttpServletRequest request,int id){

		Zixun bean = zixunService.selectData(" select * from t_zixun where id= "+id).get(0);

		request.setAttribute("bean", bean);

		request.setAttribute("title", "查看详情");

		return "zixun/zixunshow.jsp";

	}
	
	
	

	
	
	//旅游信息列表
	@RequestMapping("/tourlist.do")
	public String tourlist(HttpServletRequest request,String pagenum,String biaoti,String city){

		String url = "tourlist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_tour where  ");
		
		//查询条件返回页面
		if (biaoti != null && !"".equals(biaoti)) {

			sb.append(" biaoti like '%"+biaoti+"%' ");
			sb.append(" and ");
			
			request.setAttribute("biaoti", biaoti);
		}
		
		if (city != null && !"".equals(city)) {

			sb.append(" city like '%"+city+"%' ");
			sb.append(" and ");
			
			request.setAttribute("city", city);
		}
		

		sb.append(" 1=1 order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Tour>> map = tourService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Tour> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "tour");

		request.setAttribute("title", "旅游信息管理");


		return "tour/tourlist.jsp";

	}


	//跳转到添加旅游信息页面
	@RequestMapping("/touradd.do")
	public String touradd(HttpServletResponse response,HttpServletRequest request){
		
		request.setAttribute("url", "touradd2.do");

		request.setAttribute("title", "添加旅游信息");

		return "tour/touradd.jsp";

	}


	//添加旅游信息操作
	@RequestMapping("/touradd2.do")
	public void touradd2(HttpServletRequest request,HttpServletResponse response,Tour bean,MultipartFile prodFile){
		
		if(prodFile==null || prodFile.getSize()<=0 ){
			this.getPrintWriter(response).print("<script language=javascript>alert('旅游图片不能为空');" +
			"window.location.href='touradd.do';</script>");
			return;
		}
		
		String pic =  Util.uploadFile(request, prodFile);
		
		bean.setPic(pic);
		
		bean.setBuys(0);
		bean.setCtime(Util.getRiqi());
		bean.setTuijian("未推荐");
		bean.setVist(0);
		
		tourService.insertBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='tourlist.do';</script>");
	}


	//跳转到修改旅游信息页面
	@RequestMapping("/tourupdate.do")
	public String tourupdate(HttpServletRequest request,int id){

		Tour bean = tourService.selectData(" select * from t_tour where id= "+id).get(0);
		
		request.setAttribute("bean", bean);

		request.setAttribute("url", "tourupdate2.do?id="+id);
		
		request.setAttribute("title", "修改旅游信息");

		return "tour/tourupdate.jsp";

	}

	//修改旅游信息操作
	@RequestMapping("/tourupdate2.do")
	public void tourupdate2(HttpServletRequest request,HttpServletResponse response,Tour bean,MultipartFile prodFile){

		if(prodFile!=null && prodFile.getSize()>0){
			String pic =  Util.uploadFile(request, prodFile);
			
			bean.setPic(pic);
		}
		
		tourService.updateBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='tourlist.do';</script>");
	}


	//删除操作
	@RequestMapping("/tourdelete.do")
	public void tourdelete(HttpServletResponse response,int id){

		tourService.deleteBean(id);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='tourlist.do';</script>");
	}



	//跳转到查看详情页面
	@RequestMapping("/tourshow.do")
	public String tourshow(HttpServletRequest request,int id){

		Tour bean = tourService.selectData(" select * from t_tour where id= "+id).get(0);

		request.setAttribute("bean", bean);

		request.setAttribute("title", "查看详情");

		return "tour/tourshow.jsp";

	}
	
	
	
	//推荐旅游信息操作
	@RequestMapping("/tourupdate3.do")
	public void tourupdate3(HttpServletRequest request,HttpServletResponse response,int id){

		Tour bean = tourService.selectData(" select * from t_tour where id= "+id).get(0);
		
		bean.setTuijian("已推荐");
		
		tourService.updateBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='tourlist.do';</script>");
	}
	
	
	
	//取消推荐操作
	@RequestMapping("/tourupdate4.do")
	public void tourupdate4(HttpServletRequest request,HttpServletResponse response,int id){

		Tour bean = tourService.selectData(" select * from t_tour where id= "+id).get(0);
		
		bean.setTuijian("未推荐");
		
		tourService.updateBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='tourlist.do';</script>");
	}
	
	
	//处理中订单列表
	@RequestMapping("/orderslist.do")
	public String orderslist(HttpServletRequest request,String pagenum,String ordersid,String username){

		String url = "orderslist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_orders where  ");
		
		//查询条件返回页面
		if (username != null && !"".equals(username)) {

			sb.append(" username like '%"+username+"%' ");
			sb.append(" and ");
			
			request.setAttribute("username", username);
		}
		
		if (ordersid != null && !"".equals(ordersid)) {

			sb.append(" ordersid like '%"+ordersid+"%' ");
			sb.append(" and ");
			
			request.setAttribute("ordersid", ordersid);
		}
		

		sb.append(" staus='处理中' order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Orders>> map = ordersService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Orders> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "orders");

		request.setAttribute("title", "处理中订单");


		return "orders/orderslist.jsp";

	}


	

	//跳转到处理订单页面
	@RequestMapping("/ordersupdate.do")
	public String ordersupdate(HttpServletRequest request,int id){

		Orders bean = ordersService.selectData(" select * from t_orders where id= "+id).get(0);
		
		request.setAttribute("bean", bean);

		request.setAttribute("url", "ordersupdate2.do?id="+id);
		
		request.setAttribute("title", "处理订单");

		return "orders/ordersupdate.jsp";

	}

	//处理中订单操作
	@RequestMapping("/ordersupdate2.do")
	public void ordersupdate2(HttpServletRequest request,HttpServletResponse response,int id ){

		String riqi = request.getParameter("riqi");
		
		Orders bean = ordersService.selectData(" select * from t_orders where id= "+id).get(0);
		
		
		bean.setStaus("处理成功");
		bean.setRiqi(riqi);
		
		
		ordersService.updateBean(bean);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='orderslist.do';</script>");
	}


	//删除操作
	@RequestMapping("/ordersdelete.do")
	public void ordersdelete(HttpServletResponse response,int id){

		ordersService.deleteBean(id);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='orderslist.do';</script>");
	}



	//跳转到查看详情页面
	@RequestMapping("/ordersshow.do")
	public String ordersshow(HttpServletRequest request,int id){

		Orders bean = ordersService.selectData(" select * from t_orders where id= "+id).get(0);

		request.setAttribute("bean", bean);

		request.setAttribute("title", "查看详情");

		return "orders/ordersshow.jsp";

	}
	
	
	//订单查询列表
	@RequestMapping("/orderslist2.do")
	public String orderslist2(HttpServletRequest request,String pagenum,String ordersid,String username){

		String url = "orderslist2.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_orders where  ");
		
		//查询条件返回页面
		if (username != null && !"".equals(username)) {

			sb.append(" username like '%"+username+"%' ");
			sb.append(" and ");
			
			request.setAttribute("username", username);
		}
		
		if (ordersid != null && !"".equals(ordersid)) {

			sb.append(" ordersid like '%"+ordersid+"%' ");
			sb.append(" and ");
			
			request.setAttribute("ordersid", ordersid);
		}
		

		sb.append(" 1=1 order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Orders>> map = ordersService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Orders> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "orders");

		request.setAttribute("title", "订单查询");


		return "orders/orderslist2.jsp";

	}
	
	
	
	
	//旅游评价列表
	@RequestMapping("/pingjialist.do")
	public String pingjialist(HttpServletRequest request,String pagenum,String pcontent,String username){

		String url = "pingjialist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_pingjia where  ");
		
		//查询条件返回页面
		if (username != null && !"".equals(username)) {

			sb.append(" username like '%"+username+"%' ");
			sb.append(" and ");
			
			request.setAttribute("username", username);
		}
		
		if (pcontent != null && !"".equals(pcontent)) {

			sb.append(" pcontent like '%"+pcontent+"%' ");
			sb.append(" and ");
			
			request.setAttribute("pcontent", pcontent);
		}
		

		sb.append(" status='已评价' order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Pingjia>> map = pingjiaService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Pingjia> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "pingjia");

		request.setAttribute("title", "旅游评价管理");


		return "pingjia/pingjialist.jsp";

	}


	

	//删除操作
	@RequestMapping("/pingjiadelete.do")
	public void pingjiadelete(HttpServletResponse response,int id){

		pingjiaService.deleteBean(id);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='pingjialist.do';</script>");
	}
	
	
	
	
	//注册用户列表
	@RequestMapping("/userlist.do")
	public String userlist(HttpServletRequest request,String pagenum,String username){

		String url = "userlist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_user where  ");
		
		//查询条件返回页面
		if (username != null && !"".equals(username)) {

			sb.append(" username like '%"+username+"%' ");
			sb.append(" and ");
			
			request.setAttribute("username", username);
		}
		
		sb.append(" role=2 order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<User>> map = userService.selectBeanMap(currentpage,pageSize,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<User> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		//添加，更新，删除等按钮
		request.setAttribute("url2", "user");

		request.setAttribute("title", "注册用户管理");


		return "user/userlist.jsp";

	}


	

	//删除操作
	@RequestMapping("/userdelete.do")
	public void userdelete(HttpServletResponse response,int id){

		userService.deleteBean(id);


		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='userlist.do';</script>");
	}
	
	
	
}












